<template>
  <div class="releaseVersion">
    <div style="width:100%;margin-bottom:21px;">
      <el-input v-model="name" clearable placeholder="请输入名称" style="width:15%;" @keyup.enter.native="handleFilter"/>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button size="medium" type="info" @click="clearParams">清空</el-button>
      <el-button type="primary" style="float:right;" @click="dialog = true; form={}; flag=1">新增版本</el-button>
    </div>
    <div class="allsearch">
      <eltable
        :table-label="tableLabel"
        :table-data="tableData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
      >
        <el-table-column slot="button" label="操作" min-width="10" align="center">
          <template slot-scope="scope">
            <el-button type="primary" @click="updateVersion(scope.row)">编辑</el-button>
            <el-button type="danger" @click="deleteVersion(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </eltable>
      <pagination
        :total="total"
        :current-page="currentpage"
        :page-size="pagesize"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />

      <el-dialog :visible.sync="dialog" :title="flag===0 ? '修改版本' : '新增版本'" width="590px">
        <el-form ref="dataForm" :model="form" :rules="rule">
          <el-form-item label-width="120px" label="版本名称" prop="name">
            <el-input v-model="form.name" style="width:300px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialog = false;$refs.dataForm.resetFields">取 消</el-button>
          <el-button type="primary" @click="create()">保 存</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import Eltable from '@/components/Eltable'
  import Pagination from '@/components/Pagination' // 分页组件
  import {getTenantId, getUserName} from '@/utils/auth'
  import {addVersion, deleteVersion, getVersionList, updateVersion} from '@/api/subject/category' // 数据接口
  export default {
    components: {
      Eltable,
      Pagination
    },
    data() {
      const checkName = (rule, value, callback) => {
        const res = this.tableData.find(item => {
          return item.name === value
        });
        if (!res) {
          callback()
        } else {
          callback(new Error('版本已存在,请重新输入'))
        }
      };
      return {
        total: 0,
        currentpage: 1,
        pagesize: 10,
        tableData: [],
        filterTableData: [],
        tableLabel: [
          {
            label: 'ID', width: '5', prop: 'id'
          }, {
            label: '名称', width: '20', prop: 'name'
          }, {
            label: '所属课程', width: '10', prop: 'courseName'
          }, {
            label: '添加时间', width: '20', prop: 'createTimeTimeStr'
          }
        ],
        dialog: false,
        flag: 0,
        form: {},
        rule: {
          name: [
            {required: true, message: '请输入版本名称', trigger: 'blur'},
            {validator: checkName, trigger: 'blur'}
          ]
        },
        name: ''
      }
    },
    mounted() {
      this.getVersionData()
    },
    methods: {
      getVersionData() {
        getVersionList(getTenantId()).then(res => {
          this.tableData = res.data.items;
          this.filterTableData = res.data.items;
          this.total = res.data.items.length
        })
      },
      updateVersion(row) {
        this.form = Object.assign({}, row);
        this.dialog = true;
        this.flag = 0
      },
      deleteVersion(row) {
        this.$confirm('是否确定删除该版本', '删除版本', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteVersion(row.id).then(res => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            });
            this.getVersionData()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消'
          })
        })
      },
      create() {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.dialog = false;
            if (this.form.id) {
              updateVersion(this.form).then(res => {
                this.$notify({
                  title: '成功',
                  message: '修改成功',
                  type: 'success',
                  duration: 2000
                });
                this.getVersionData()
              })
            } else {
              this.form.tenantId = getTenantId();
              this.form.creator = getUserName();
              addVersion(this.form).then(res => {
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                });
                this.getVersionData()
              })
            }
          }
        })
      },
      handleSizeChange(val) {
        this.currentpage = 1;
        this.pagesize = val.value;
        if (this.pagesize >= 20) {
          scrollTo(50, 800)
        }
      },
      handleCurrentChange(val) {
        this.currentpage = val.value;
        if (this.pagesize >= 20) {
          scrollTo(50, 800)
        }
      },
      handleFilter() {
        this.tableData = this.filterTableData.filter(item => {
          if (this.name && item.name.toLocaleLowerCase().indexOf(this.name.toLocaleLowerCase()) < 0) return false;
          return true
        })
      },
      clearParams() {
        this.name = '';
        this.getVersionData()
      }
    }
  }
</script>

<style lang="scss">
  .releaseVersion {
    .allsearch {
      margin-top: 0;
      padding: 0;

      .search-content {
        width: 100%;
      }

      .el-table td:nth-child(2) .cell {
        text-align: left
      }
    }

    .addprogram {
      width: 100%;
      padding: 0 0 21px 0;
      height: 36px;

      .el-button {
        float: right;
      }
    }
  }
</style>
